package com.example.aussiehealthhelper;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class DrugAdapter extends RecyclerView.Adapter<DrugAdapter.ViewHolder> {

    private List<Drug> drugList;
    private final DrugClickListener clickListener;

    public interface DrugClickListener {
        void onDrugClick(Drug drug);
    }

    public DrugAdapter(List<Drug> drugList, DrugClickListener clickListener) {
        this.drugList = drugList;
        this.clickListener = clickListener;
    }

    public void updateData(List<Drug> newList) {
        drugList = newList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_drug, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Drug drug = drugList.get(position);
        setupDrugDisplay(holder, drug);
        String indication = drug.getIndication();
        if (indication == null || indication.trim().isEmpty()) {
            holder.indication.setText("暂无适应症信息");
        } else {
            holder.indication.setText(indication);
        }

        //设置收藏图标
        if (drug.getIsFavorite()) {
            holder.favoriteIcon.setVisibility(View.VISIBLE);
            holder.favoriteIcon.setImageResource(R.drawable.ic_favorite_filled);
        } else {
            holder.favoriteIcon.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(v -> clickListener.onDrugClick(drug));
    }

    private void setupDrugDisplay(ViewHolder holder, Drug drug) {
        String drugName = drug.getNameCN();
        String similar = drug.getSimilarDrugs();

        //没有相似药品：只显示药品名称
        if (similar == null || similar.trim().isEmpty()) {
            holder.drugName.setText(drugName);
            holder.similarDrug.setVisibility(View.GONE);
            return;
        }

        //获取第一个相似药品名称
        String[] similarDrugs = similar.split("[,;、]");
        String firstSimilar = similarDrugs.length > 0 ? similarDrugs[0].trim() : similar.trim();

        //药品名称----澳洲相似药品
        String combinedText = drugName + " ----- " + firstSimilar;
        SpannableString spannable = new SpannableString(combinedText);

        int start = drugName.length();
        spannable.setSpan(
                new RelativeSizeSpan(0.85f),
                start,
                combinedText.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        );

        holder.drugName.setText(spannable);
        holder.similarDrug.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return drugList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView drugName;
        TextView similarDrug;
        TextView indication;
        ImageView favoriteIcon;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            drugName = itemView.findViewById(R.id.tvDrugName);
            similarDrug = itemView.findViewById(R.id.tvSimilarDrug);
            indication = itemView.findViewById(R.id.tvIndication);
            favoriteIcon = itemView.findViewById(R.id.ivFavorite);
        }
    }
}